package vip.xiaonuo.modular.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.modular.cms.entity.*;
import vip.xiaonuo.modular.cms.service.*;
import vip.xiaonuo.modular.cms.vo.ArticleVO;
import vip.xiaonuo.modular.cms.vo.PageVO;
import vip.xiaonuo.modular.cms.vo.ProductVO;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ProjectName: Hitech_CMS
 * @Package: vip.xiaonuo.modular.cms.service.impl
 * @ClassName: PublicApiServiceImpl
 * @Author: liubaihan
 * @Description: CMS开放API接口实现类
 * @Date: 2021/8/23 14:23
 */
@Service
public class PublicApiServiceImpl implements PublicApiService {

    @Autowired
    ArticleService articleService;

    @Autowired
    ProductService productService;

    @Autowired
    PageService pageService;

    @Autowired
    CatalogService catalogService;

    @Autowired
    ArticleContentService articleContentService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SysUserService sysUserService;

/**************************************************************************** 栏目接口 *****************************************************************/
    @Override
    public List<Catalog> getCatalogInfo() {
        LambdaQueryWrapper<Catalog> catalogLambdaQueryWrapper =  new LambdaQueryWrapper<>();
        catalogLambdaQueryWrapper.eq(Catalog::getIsView,"Y");
        return catalogService.list(catalogLambdaQueryWrapper);
    }

    @Override
    public List<Catalog> getCatalogChildrenById(String id) {

        LambdaQueryWrapper<Catalog> catalogLambdaQueryWrapper =  new LambdaQueryWrapper<>();
        catalogLambdaQueryWrapper.eq(Catalog::getIsView,"Y");
        List<Catalog> list = catalogService.list(catalogLambdaQueryWrapper);

        return this.iterateCatalog(list,Long.valueOf(id),new ArrayList<>());
    }

    /**
     * 递归查询栏目信息
     *
     * @param catalogs:
     * @param pid:
     * @param catalogChild:
     * @return: java.util.List<vip.xiaonuo.modular.cms.entity.Catalog>
     * @author: liubaihan
     * @date: 2022/9/15 16:55
     */
    public List<Catalog> iterateCatalog(List<Catalog> catalogs, Long pid,List<Catalog> catalogChild) {
        List<Catalog> catalogChildInfo = catalogs.stream().filter(e -> e.getPid().equals(pid)).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(catalogChildInfo)){
            catalogChild.addAll(catalogChildInfo);
            catalogChildInfo.forEach(e -> {
                this.iterateCatalog(catalogs,e.getId(),catalogChild);
            });
        }
        return catalogChild;
    }

/**************************************************************************** 文章接口 *****************************************************************/
    @Override
    public ArticleVO getArticleInfoByCatalog(String id) {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(Article::getCatalogId,id);
        articleLambdaQueryWrapper.eq(Article::getStatus,"1");
        Article article = articleService.getOne(articleLambdaQueryWrapper);

        if(article != null){
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,article.getId());
            ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
            articleVO.setArticleContent(content.getContent());
            return articleVO;
        }

        return null;
    }


    @Override
    public ArticleContent getArticleContentByArticleId(String id) {
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,id);
        ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
        return content;
    }

    @Override
    public Page<ArticleVO> getArticlePageList(Integer pageNum, Integer pageSize, String searchText,String catalogId) {
        String domain = (String)request.getAttribute("domain");

        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(searchText)){
            lambdaQueryWrapper.like(Article::getTitle,searchText);
        }

        // 如果栏目ID不为空
        if(StrUtil.isNotBlank(catalogId)){
            List<String> catalogIds = new ArrayList();
            catalogIds.add(catalogId);

            List<Catalog> catalogs = this.getCatalogChildrenById(catalogId);
            if(CollectionUtils.isNotEmpty(catalogs)){
                List<String> childCatalogIds = catalogs.stream().map(e -> String.valueOf(e.getId())).collect(Collectors.toList());
                catalogIds.addAll(childCatalogIds);
            }
            lambdaQueryWrapper.in(Article::getCatalogId,catalogIds);
        }

        lambdaQueryWrapper.eq(Article::getStatus,"1");

        Page<Article> page = new Page<Article>(pageNum,pageSize);
        Page<Article> articlePage = articleService.page(page, lambdaQueryWrapper);

        List<ArticleVO> articleVOS = new ArrayList<>();
        articlePage.getRecords().forEach(article -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleVO.setUrl(domain + "/getArticleContentByArticleId/" + article.getId() );
            articleVOS.add(articleVO);
        });

        Page<ArticleVO> pageArticleVO = new Page<ArticleVO>(articlePage.getCurrent(),articlePage.getSize(),articlePage.getTotal());
        pageArticleVO.setRecords(articleVOS);
        return pageArticleVO;
    }

    @Override
    public ArticleVO getArticleInfoById(String id) {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(Article::getId,id);
        articleLambdaQueryWrapper.eq(Article::getStatus,"1");
        Article article = articleService.getOne(articleLambdaQueryWrapper);

        if(article != null){
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(article,articleVO);
            articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,article.getId());
            ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
            articleVO.setArticleContent(content.getContent());
            return articleVO;
        }

        return null;
    }

    /**************************************************************************** 产品接口 *****************************************************************/
    @Override
    public ProductVO getProductInfoByCatalog(String id) {
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getCatalogId,id);
        productLambdaQueryWrapper.eq(Product::getStatus,"1");
        Product product = productService.getOne(productLambdaQueryWrapper);

        if(product != null){
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product,productVO);
            articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,product.getId());
            ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
            productVO.setArticleContent(content != null ? content.getContent():"");
            return productVO;
        }

        return null;
    }

    @Override
    public ArticleContent getProductContentByProductId(String id) {
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,id);
        ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
        return content;
    }

    @Override
    public Page<ProductVO> getProductPageList(Integer pageNum, Integer pageSize, String searchText, String catalogId) {

        String domain = (String)request.getAttribute("domain");

        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(searchText)){
            lambdaQueryWrapper.like(Product::getTitle,searchText);
        }

        // 如果栏目ID不为空
        if(StrUtil.isNotBlank(catalogId)){
            List<String> catalogIds = new ArrayList();
            catalogIds.add(catalogId);

            List<Catalog> catalogs = this.getCatalogChildrenById(catalogId);
            if(CollectionUtils.isNotEmpty(catalogs)){
                List<String> childCatalogIds = catalogs.stream().map(e -> String.valueOf(e.getId())).collect(Collectors.toList());
                catalogIds.addAll(childCatalogIds);
            }
            lambdaQueryWrapper.in(Product::getCatalogId,catalogIds);
        }
        lambdaQueryWrapper.eq(Product::getStatus,"1");

        Page<Product> page = new Page<Product>(pageNum,pageSize);
        Page<Product> productPage = productService.page(page, lambdaQueryWrapper);

        List<ProductVO> productVOS = new ArrayList<>();
        productPage.getRecords().forEach(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product,productVO);
            productVO.setUrl(domain + "/getProductContentByProductId/" + product.getId() );
            productVOS.add(productVO);
        });

        Page<ProductVO> pageProductVO = new Page<ProductVO>(productPage.getCurrent(),productPage.getSize(),productPage.getTotal());
        pageProductVO.setRecords(productVOS);
        return pageProductVO;
    }

    @Override
    public ProductVO getProductInfoById(String id) {
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getId,id);
        productLambdaQueryWrapper.eq(Product::getStatus,"1");
        Product product = productService.getOne(productLambdaQueryWrapper);

        if(product != null){
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product,productVO);
            articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,product.getId());
            ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
            productVO.setArticleContent(content != null ? content.getContent():"");
            return productVO;
        }

        return null;
    }

    /**************************************************************************** 页面接口 *****************************************************************/

    @Override
    public PageVO getPageInfoByCatalog(String id) {

        LambdaQueryWrapper<vip.xiaonuo.modular.cms.entity.Page> pageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ArticleContent> articleContentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pageLambdaQueryWrapper.eq(vip.xiaonuo.modular.cms.entity.Page::getCatalogId,id);
        pageLambdaQueryWrapper.eq(vip.xiaonuo.modular.cms.entity.Page::getStatus,"1");
        vip.xiaonuo.modular.cms.entity.Page page = pageService.getOne(pageLambdaQueryWrapper);

        if(page != null){
            PageVO pageVO = new PageVO();
            BeanUtils.copyProperties(page,pageVO);
            articleContentLambdaQueryWrapper.eq(ArticleContent::getArticleId,page.getId());
            ArticleContent content = articleContentService.getOne(articleContentLambdaQueryWrapper);
            pageVO.setArticleContent(content.getContent());
            return pageVO;
        }

        return null;
    }

/**************************************************************************** 通知接口 *****************************************************************/

    @Override
    public List<Notice> getNoticeListByType(String type) {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getNoticeType,type);
        lambdaQueryWrapper.eq(Notice::getStatus,"1");
        lambdaQueryWrapper.orderByDesc(Notice::getPublishTime);
        List<Notice> list = noticeService.list(lambdaQueryWrapper);
        return list;
    }

    @Override
    public String getUserNameByUserId(Long id) {
        return sysUserService.getNameByUserId(id);
    }
}


